package com.backend.demo.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.backend.demo.baseDAO.BaseDao;
import com.backend.demo.entities.Role;
import com.backend.demo.entities.Role_Permission;
import com.backend.demo.util.DateTimeUtil;
import com.backend.demo.util.StringUtils;

/**
 * 角色DAO
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("unchecked")
@Repository
public class RoleDao extends BaseDao<Role, Long> {

	@Resource(name="role_Permission_Dao")
	private Role_Permission_Dao role_Permission_Dao;

	/**
	 * 得到所有的角色信息
	 * 
	 * @return
	 */
	public List<Role> getAllRoles() {
		List<Role> roles =getListByHQL("from Role");
		return roles;
	}

	/**
	 * 多条件查询
	 * 
	 * @param roleName
	 * @param createtime
	 * @return
	 */
	public List<Role> getRolesByCondition(Role role) {
		StringBuffer stringBuffer = new StringBuffer("from Role where 1=1");
		if (StringUtils.isNotEmpty(role.getRoleName())) {
			stringBuffer.append("and roleName like '%" + role.getRoleName() + "%'");
		}
		if (StringUtils.isNotEmpty(role.getRoleNameen())) {
			stringBuffer.append("and roleNameen like '%" + role.getRoleNameen() + "%'");
		}
		if (role.getCreatetime() != null) {
			String format = DateTimeUtil.format("yyyy-MM-dd HH:mm:ss", role.getCreatetime());
			stringBuffer.append("and createtime >='"+format+"'");
		}
		List<Role> roles = getListByHQL(stringBuffer.toString());
		return roles;
	}
	
	/**
	 * 根据角色ID查询角色信息
	 * @param roleID
	 * @return
	 */
	public Role getRoleById(long roleID){
		String hql = "from Role where roleID = ?";
		return getOneByHQL(hql, roleID);
	}

	/**
	 * 添加一个角色
	 * 
	 * @param role
	 */
	public int addRole(Role role) {
		role.setCreatetime(new Date());
		role.setUpdatetime(new Date());
		return save(role);
	}

	/**
	 * 更新角色信息
	 * 
	 * @param role
	 */
	public int  updateRole(Role role) {
		String hql = "update Role set roleName = ? ,roleNameen = ?, updatetime = ? where roleID = ?";
		Date date = new Date();
		return updateOrDeleteByHql(hql, role.getRoleName(),role.getRoleNameen(), date, role.getRoleID());
	}

	/**
	 * 删除一个或者多个角色
	 * 
	 * @param ids
	 */
	public int delete(Long[] ids) {
		StringBuffer stringBuffer = new StringBuffer("delete from Role ");
		for (int i = 0; i < ids.length; i++) {
			if (i == 0) {
				stringBuffer.append("roleID = " + ids[i]);
			} else {
				stringBuffer.append("or roleID = " + ids[i]);
			}
		}
		return updateOrDeleteByHql(stringBuffer.toString());
	}

	/**
	 * 给角色授予一个或者多个权限
	 * 
	 * @param roldID
	 * @param permissionID
	 */
	public void grantPermissionForRole(Long roleID, Long[] permissionID) {
		List<Role_Permission> permissions = new ArrayList<Role_Permission>();
		for (int i = 0; i < permissionID.length; i++) {
			Role_Permission role_Permission = new Role_Permission();
			role_Permission.setRoleID(roleID);
			role_Permission.setPermissionID(permissionID[i]);
			//格式化日期
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm");
			String date = dateFormat.format(new Date());
			try {
				role_Permission.setCreatetime(dateFormat.parse(date));
				role_Permission.setUpdatetime(dateFormat.parse(date));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			permissions.add(role_Permission);
		}
		role_Permission_Dao.saveOrUpdateBatch(permissions);
	}
}
